Re-structure some code