Thanks to @Pitcrawler for refactoring/correcting the code Closes #446 Closes #5164 (not complete)
f4075f0f94