huge code refactoring