Optimised the back-end by making the queries shorter and reusing more code