Reduced crashes but much more ugly code