Changed to using try-with-resources for statements
This ensures that all statements get closed properly