refactored and reorganized