replace all .close() calls with java7 try/finally blocks + minor refactoring around