Access token database

Refactored the code according to the comments and added test cases.
Not able to understand what this method really does..