Refactoring for error handling, lots more error handling