Moved some logic out of server.R