Rework database and rewrite order matching