REFACTOR error handling