refactored code, advice from Alex