fixed write code in most cases