Moved code blocks to functions