Fix code smell and runtime error