fixed optimized drawing routine and factored some code.