separate stuff out and fix a bunch of bugs