bug fixing and code refactoring