Changed implementation of heuristic function.