Initial implementation. Lots of cleanup left