fixup! factor error check into a function