Refactor some duplicated code. Harmonise logging messages.
Refactor duplicated code in tests into run_ofSM() to simplify tests.