made some fixes, however there is still a NPE to fix.