Added error checking and more tests