Make `type` function better

Multiple fixes to the `type` function to make it run much faster and behave better.

This includes adding a `-p` flag to the `command` builtin, so we can avoid invoking `which` in the common case for `type`.