Update error message and refactor the code a little