Added some null checks and slightly beefed up a test.