Re-ordered some code for clarity.