added some comments and pseudocode