refactored and clean up