Initial implementation. Seems to work.