Refactoring of working code