Main logic.