replace resused code with functions