code clean up and credit to Madcomponets