Speed improvements replacing for loops with array_map() where possible