bug fixes for file writing