Cleaned up login code and added better documentation for it.