Added some missing close() call. Ashamed. Don't judge me. Please.