lots of database optimization modifications