Improved DMD support

The changes here are mostly bug fixes.