Bug fix, refactoring