Small code reorganisation and cleanups.

A bunch a small changes.