Clean up cross products that were being recomputed and only compute the component needed.
Move functions so they are defined before they are used.
Remove isNaN checks. Instead, prevent divide by zero.