gsub: support for table and function replacements. Some more cleanup.