A complete rewrite, still there are a few TODOs to fix esp. hiv/console.