Moved the code to a function