Major code refactorization