Improve error handling greatly