V1

Correcting some bugs and optimazing the code