moved total to inside the loop