Added addDocuments method and fixed file descriptor leaks