Factor some common repeated code
Some tidying of code