Added sanity checks to addHandler and removeHandler.