Re-factor login for foundation