alot of refactoring, a few small bugs.