Refactoring, organization, and code cleanup