Various bug fixes following the refactoring