Fixed all bugs due to refactoring