Refactor to more direclty use built-in functions