Bug fixes to prevent lock up and segfaults.