Added memory optimizations to graphics-related routines