Big refactoring to the code