Refactor builing logic.