refactored and organized code