Refactoring the code - First pass

1] Added helper functions for the various heuristics
2] Improved the documentation
3] Rewrote the second heuristic according to a new algo