Fix for #565

Also removed unused open statements and made moveNodes use a pattern
type test rather than a blind dynamic cast.
Merge pull request #569 from 7sharp9/master

Fix for #565