Refactored login and storage logic into a separate function.

Also fixed search to return last matched record in the list. (this isn't
phrased correctly though)