cleaned up local var use

Cleaned up the local variable use. Some local variables weren't being called after declared (like xx, cx, cy) and some new variables were being used without first being declared using var (c1, c2, d3 always, and sometimes all the others if the condition was false).