small improvement of the UI hook code

- Just adding some error handling and smoothed the code