fixed bug with writing data to file