Refactored the constructor to rtemove duplicated code.