Some small fixes and optimizations