Fix bugs that caused crashes.