thanks to chinajade for some better looking code