doc and a bit of code deduplication

Signed-off-by: Mattias Andrée <maandree@operamail.com>