Solving leak memory problem