Check for addresses first.  Renamed several variables to elucidate their meaning.  Reordered function declarations to improve readability.