Improved the program's speed and commented the code