Moved some code to functions

Easier to mantain