Refactored graph drawing to a function