Solve array index out of bounds probem in nearest_search.

Should fix #39.