Fixed possible memory leak when working with different tables
Code prettyfied