Complete rewrite, breaking down into components