added more error checking code