optimized shadow code