Made code more self contained by making copies of functions