Added error checking to make things flow more smoothly.