Dirty first attempt that basically works.