Added lots of null checks, many other code stuff.