refactored code a bit