Added support for nested loops and lowerd memory consumption outside of loops.