My feeble attempt at optimizing the drawing code