Cleaned up and moved a few things around -- I really need to rewrite this.