started refactoring error handling