Added methods to encapsulate looping to simplify the code