complete refactor. PITA