Fixed variables with typos. Added nested loop.