Rewrite of build system and major cleanups