Major internal refactor

Building and passing (limited) tests