Added comments and error checking