Final version

Moved some code snippets to improve structure and functions