Move some code around for clarity.