Improve error check