minor refactoring and fix bug on session locking