first version of working stencil optimization