refactored walk function