Some refactorings and cleanups