Refactor command serialization.