Fixed a variety of minor problems and cleaned up error handling