New code for FOR- more intelligent