Refactored code a bit