Fixes issue 6984. Fixes issue 6972.
Huge megacommit because a lot of things needed to be modified to make this possible.