Fix for #565

Also removed unused open statements and made moveNodes use a pattern
type test rather than a blind dynamic cast.