Added comments to every function and restyled code a bit to be more compliant with judge from nr-apps.com