fixed bug in write_internal