small optimizations + safety improvements