Greatly improved build system and small refactor