Fixed memory leak
Code tidying