Heuristics added, minor refactoring