Refactor duplicate code

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