Some modifications to improve the velocity

Tried to use the gprof to get some hints on where to improve.