bug fixes and error handling