Improve code and avoid null pointer