Fix nil pointer bug