fix #1, other optimizations