Fixes to two potential memory usage issues
Fix for race condition