Fixing FileIO and memory leaks