updated main logic