Reordered the code a bit for readability.