fixed error checking bug