fixed writeCube function