refactered script for better readability