added error checking and error codes