did a bunch of refactoring and probably broke all the things. added lots of comments