code cleanup and mild refactoring