reorganize, fix up things, add functions

It was time to fix up this spaghetti I made.