reorganized and fixed nullpointer