Added the table name to query and added tests
Fixed some bugs pointed out by test suite