Refined code for database