Cleaned up horridly messy code