Major refactor/cleanup.