fixed writeCube function (even Z transitions)