Used wrong column name in SQL code.
Renamed variables.
Moved some code around.