Closes #12, thanks to @daplay for the suggestion and code.