fixed a couple of scoping issues and added swapping layers