Moving some logic out of html into js