Update code style and fix small threading bug.