more delete attempts
Tidy up the code and add docstrings.