Some refactorings and code cleanups