refactored, but needs further refactoring