Big refactoring following the introduction of sm-integrate-with macro.