Refactor code duplication

Signed-off-by: Marc Delisle <marc@infomarc.info>