Correct simplification algorithm, fix some bugs