Significantly improved error testing and function documentation