WIP, most functions ported to C++ from self-hosted