Fixed dispose order to avoid crashes