Some optimizations and crash fix