Implements all heuristics. Fixed bugs. Loads mainFunction as a normal function.