Bug fixes with a bunch of hacks.. major refactor needed