Major cleaning on code

The code was improved so that it is more readable and manipulable.