err handling and bug fixes