refactored and some initial testing