First attempt to refactoring code