fixed a few error checking bugs