Removed bugs, code refactoring