First working version after refactoring