Refactoring. Further organization of the code