........S. [ZBXNEXT-2321] rearranged code, got rid of a memory leak, general style improvements