Shifted some reusable code around to where it can be reused.