error handling refactored