refactored drawing function