Some code cleanup/refactoring