heuristic hugely refactored and optimised