Fixed syntax errors and misplaced flow logic