I've rewritten it for performance so need to use .each (which unfortunately can be very slow)