Fixed memory leaks in all functions but scheduler