moved some code from gramazon