Cleanup of code to prepare for multi-thread support