Refactors error handling and resolves #5