Fixed the error checking bugs