existing codes in c rewritten with new logic