Replaced range(len(X)) idiom with enumerate().
Refactored ListKeys/SearchKeys to maximise use of common functions.