moved plot creation to function