made a few changes and did error checking, I think i handled all cases