Updated to fix random bugs after refactoring