Bad code refactored