Re-add changes from faulty revision merge. + bonus tests.