Various fixes and cleaner error handling.