Debugged k-nearest neighbors and refactoring