I did optimize it, but didn't consolidated into one function