fixed implementation and added some comments