Does this work if there are duplicate points? Consider particularly the case where inp[best] has a duplicate - that tends to really mess things up. It's not the end of the world if it doesn't handle duplicates, but you should document it.