Fixes for various bugs detected recently