added variables and loops to make it easier to port to haswell