Refactor magento script, add sources, minor fixes