Bug fixes, minor features and optimizations