Fixed bugs in saving traces to file