Refactor and clean up binary script