Removed some redundant code and restructured bits.