add database functions and fix errors