Added error checking function