Added messages, globalFunctions-TEMPLATE, and optimized SQL queries