Move error checking to a new file