Refactored script for general use