moved error handling to correct function