moved error handling into a helper