Refactored SQL queries and renamed some variables