Use new style arrays and add a helper method.

Refactor out some duplicated code into a helper method. Use pass-by-ref
arguments to reduce time spent copying data into memory.