Accepting suggestion from azisaka and refactoring into a single loop :P