Fixed rescale,some array loops, and optimized for less memory