refactoring tests

added null checks
method extraction for better readabilty