- fixing NPE
- minor refactoring