Refactore code: reduce code repetition.