Really messy original code dump.