Added comments and titles in front of each loop for better understanding of code