refactoring plotting function