Refactoring, bug fixing