fixed possible NPE
Refactored a method part