Fix NullPointerException and add some test cases


Fixes #76