Made changes to problem for to reduce the number of loops to one.