fixes and refactors a big bunch of stuff