Refractored the code a little.