minor cleanup and error checking