somewhat refactored