trying to clean up / fix the worst javascript i have ever seen