Optional key and null fix with tests

#9 fix with tests.