Rearranged some more code to help ensure correct behavior.