Initial fix for #280

This is the simplest thing that works, but could probably do with some refactoring