code is ugly but seems functional so far.
moving on to encryption, will clean up and verify later