Lots of changes

session['user_id'] is now defined for logged in users
lots of redundant code removed
login query is now much more efficient and takes advantage of the sql
database (instead of a for loop iterating over all users)