Added null checks
Added a null check and some test cases