Added the error checking too