Refactoring and tidy up in the build system