Refactoring and cleanup

Make sure to add ?w=1 to avoid seeing whitespace changes.  

This is mainly just readability cleanup, no functionality should change.