refactored version works as before refactoring