Made some structural changes. Code seemed too confusing.