refactored as fuck