Updated build system and some minor fixes/code factorisation.