Update code to make the test green.

The updated code need refactoring as it contains duplicated code.