refactored a bit, sorta sketchy... need to write tests.