Revised, with less array copies. I didn't know I could use a primiteve array (int[]) as a generic/template type. Awesome.