Completed initial rewrite with tests