Fixes issue 6773. Fixes issue 6774. Thanks nash67 for his help.
Huge megacommit because a lot of things needed to be modified to make this possible.