code polished, redundancy removed, logic improved