refactoring; bug fixing