Updated code - now understand the heuristic