Cleaned up threading code, part A