move code out of main.js and into layer.js