Refactor, improve, and document the deriving mechanism