fixed bug in writing out data