Create tables before functions and triggers

Should fix the bug I introduced in my previous commit.
Merge branch 'db-stuff'