sort peaks correctly. renamed bestf -> best_frequency
fixed some index syntax and off-by-ones