lots of updates, almost a complete re-write