Moved code into functions for readability