Refactored and polished up