refractoring and cleaning up code