Copied new code from PATCH-patch