Code refactoring. Final version