fixing bug and adding additional logging in error case