various small fixes and optimizations