Changed the list iteration from using pop method to using index