first try at nlogn