fixed memory/multi-thread resource access bugs