Refactored code a little