Moved some code to functions and added trap for terminal resize