LUN-670 -- some fixes and optimizations