Better error handling, bug fixes