added cache and improve redrawing functionality