rewritten from scratch, oops.