refactored to contain functions