pacemaker code refactoring

Please attentively look at this refactored code  for mistakes