Code cleanup, minor refactorings