more and more bug fixes and error checking