Added small error check and some comments