fixed main logic