working, partially refactored code