formatting and some small optimizations