optimize canvas implementation further