Improve patch algoritm using jpatterson's code