Several refactors for code reuse.