work in progress, code skeleton looks mostly correct