better error handling, bug fixes