refactored code a little bit