full code rewrite

shitload of changes. Don't remember.