Added comments and error checking functionality