Fixed Mike's absolutely HORRENDOUS code.