Major rewrite of the mod.