Moved some logic away from templates, back in the JS side