improve error checking and message