refactored and organized