Made several changes to match Dia specifications as per hall5714's review.
Same as before, rebased against master.