More or less working version
rounded out initial implementation