added DOCUMENT_HANDLE function
fixed small memleak