Refactored functions to use arrays rather than looped. Tested with timeit.