Close some resources automatically
Fix a lot of static analysis issues