refractoring code to functions