Added an error checking function