added comments for algorithm