Simplified much of the code. Removed unneeded functions/etc.