Some code rearranging and cleanup, more work on underlayer (although still very little visible effect).