Rewrote some methods to be more generic and easier to follow.