refactored code from last time