Added a more generic threshold statemachine