Refactorization and cleanup