Started major refactoring of the code