added some error checking at the end