Minor refactorings and code cleanup