refactor command to include error handling