Additional error checking and more extensive commenting