Fix $each and code optimization