added some type information, removed some unused variables, avoided computing stuff over and over again