almost refactored