fixed closure alloc. issues, refactored func.gen. code