Fixed memory leak + some more minor stuff