first complete, working implementation