foreach, sortby improvements