add assertion and refactor slightly
tidy up and add missing assertions
tidy up
this looks better