some refactoring of duplicated code.

Signed-off-by: Chris Jones <chris@jonezy.org>