Lots of refactoring and additions

Signed-off-by: John Krause <jpkrause@myopera.com>