Fixed NPE and other issues.