Rewrite patch based on djf's suggestions