Refactor command processing