Lots of rearranging and reimplementing and some new functionality added