refactoring: code as in slides