Removed unnecessary duplicate line.
Rearranged function order to make jslint happier.
Define functions before calling them.
Fixed broken condition.
Avoid potentially confusing redundant variable declarations.