Initial work

This seems to work as I intended. Further testing is required