updated comments and added some error checking