refactored code a little