Code adapted for 2 tables