added some commentary; improved performance a bit