finally working version after fixing hash access issues