Added error function and some comments