Improve error reporting and bug fixes