Initial O(n log n) algorithm done, about to debug