Fixed a bug when switching panes
Some tidying up and making things more proper