Major overhaul of database code. Functional echo handler