added more error checking