Updated patch based on totemo's suggestions