Complete implementation, needs tests.