Mostly works, needs tests.