added additional error andling as well as some code cleanup