Refactored adn added some comments plus a small variable name change