Refactoring and numbering of layers

 - Layers are numbered from 0, not from 1.
 - Smal refactoring.