Refactored adn added some comments