added some comments and checking return value from read