Tidying up a little.
Refactor some nested loops