Refactoring, using const, removing global variables, passing some new arguments.