Remove dead code, relocate some code