Combination of changes from Alexey Markevich with updates from Robbie Gemmell. See PRs for detail. This closes #4584. This closes #4540.