added more explanation to algorithm