Some optimizing, debugging,  and refactoring in draw.js