Fix exiting namespace when it's the last child (#223), clean up surrounding code