don't directly return a value in the XS routine

and handle error condition properly to avoid unexpected behaviours