Changed the backend to use two tables
Refactored to make the code nicer