refactor drawing code