code shortened, logic modified