Fixed (my) crappy initial implementation of layering and a few bugs.