Fixed algo; tests passing.