final, stripped down code for sharing