fix a NullPointerException bug and refactor thereby some parts