complete rewrite, uber fast and pretty solid