Issue #178 + minor optimizations