added better exception handling