Fix bug, but need refactoring