added some comments and simplification of the code