Rewrote majority of code.