complete code for main logic