another rewrite, ready for testing