Fixed more stuff in write function