Initial version of refactored stm code