Added -g option, created a build function that is common to all cases.