Heavily commented and tidied up mapcanvas.js