refactored code, much shorter