moved error handling into if