Major refactor. Working solution now.