Added some nullpointer checks and made client more reliable.