cleaned up and refractored code a bit
refractored code and commented further.