r8839: adding patches from Marcin. Have to still work on