a whole pile of changes thanks to @pbhandari