this is in "it appears to work" state, though I blindly copy-pasted a little more code than I'm happy with, so might benefit from a review