refactored code seems to be working