Moved plotting into separate functions
bug fixes