solved memory leak & made refactoring