reimplemented commit 4b561e3, but with better support for additional features [fixes #1 (again)]