Implemented algorithm for "leveling"