refactored and cut the big 1 function  into pieces