Various small cleanups and refactor