Added optimizations after closure. Fixed the related (found) bugs.