Simplify patch and fix bugs