Refactor to tidy up and simplify.