Added NULL checks.
Improved the test file to use average from multiple functino calls.