Various cleanups and refactoring...