A confusion occurs between two different usages of THEN i.e. the one used in conjunction with WHEN and with IF.

Parallel to changing to WHEN it would make sense to change the accompanying THEN to something like DO and leave usage of THEN to the IF statement.

A color difference in the editor makes it even more clear