Ported c1670f8 to the refactor

Unit tests are not yet working in this WIP branch