code rewrite

rewrote a lot of code