add file writes. fix bug