Impletented the main logic.