Rewrote some of the code