Completed checkCFG function