Improved the simplification code