Full refactoring. First working varian