Code cleanup; minor refactoring