Fixed NPE
Some small refactorings