Wrote error checking function