Working on functions to prevent memory leaks