refactor this
fixed error trapping