Fix warnings

Fix resource leaks etc