Sped up by moving to NumPY operations.