optimize logic

main logic