Removes unused code and refactors some code

Some cherry-picking messup that duplicated the commit, but no worries for the change set.