Initial rewrite with better implementation/API and tests