Fix bug with expiration & time, some code refactoring