Working version before refactoring