Added comments to the code
Split large if up, naming subparts