optimalizations & added QueryException
minor changes to exception handling and naming