refactor layer control code into function
Merge pull request #71 from marsmith/master

refactor layer control code into function