Further optimized database code