added comments and pseudo code