Refactor build, add low-leve threading utils