partially refactored