Refactored and merged