Fixed bugs caused by moving from methods to functions