made some new function for reading and writing, and cleane up my code. Added code for the trace file