fixed null pointer bug, optimized code.