Some refactoring and more heuristics