Modularized the logic.