working rewrite with tests. still need better CC