added a more optimized version using session keys