NPE handling / Some refactoring