applied patches from @ecwws