This is far from ideal.
PRs welcome :)