some optimizations and bug-fixes