Preallocate arrays and replace values (instead of push) to improve performance, see #38