restructured and fixed null pointer exceptions