fixes to handling of message counts in threads and some cleanup