Dialog: ported from be.net

Also includes a major refactor