Improve error checking