some optimizations

Note there might be newly introduced bugs...