working version, refactoring needed;