refactoring codes to avoid session problems