refactored error handling. and now functions are statements when possible
support both grammar and smart queries
divided event handlers from functions