Closing resources when necessary; use try-with-resources to automatically deal with opened resources; rename some funky local variables