as_hash cleanup

These commits make the as_hash implementation and use more robust and correct, as well as more obvious to readers of the code.