Speed up of anothers code with constraints on what could be changed.