Fixed bugs with writing to File.

TODO - address lock on the line where a new Thread is spawned