Script refactored after comments