Code cleanup

@funkyeah
@swstack

This change has a few different changes in it and there aren't any major feature changes (maybe a few bug fixes).  I tried to clean things up as best as possible.