Implement & test tidy function

Fixes #216