Made the error checking better.