bug fixes and improved error handling