messy code but everything seems to work. Fixed avatar issue