Bug fixes and error checking