Working version before major refactoring