-fixed a bug in the error checking function
-cleaned up the code a bit