Some bug fixes from the main branch