the code started out nice, and turned ugly as time went on... at least i tried to make it nice