Refactor background script