fixed a lot of bugs due to refactoring