added better error handlling