structured the code and commented a part of it