Refactor error handling - invalid codes used