cleaned code a little to optimize performance (*cough*) and to make it readable