Refactoring and error handling overhaul.