first complete (?) implementation, without tests