Large revision of the code

Large revision of the code