code cleaned and refractored