Added faster version of algorithm, and accompanying tests.