Improved error handling, bugfix