refractored code (ver. 1)