Code cleanups and refactorings