Fix crash, small optimizations