Refractored code a bit