First pass at implementation. Thanks to @markmakes for his help.