Reorder checks, move declaration closer to where it's needed