Reduce dependencies and add test; almost a complete rewrite