better error handling and query generation
Merge pull request #1 from clamidity/master

Update from Orignal