Analytics code cleanup/refactoring