simplified and added some comments