Second attempt at simplifying code