Just as for asin() in the out-of-plane angle calculation, you should check the argument to acos() to make sure it lies between -1.0 and +1.0.