large scale refactor

RT