added better conditional and type checking