Some refactoring and fixes to plotting