improved code to look like the algorithm in algorithmic notation