Separate main logic into Helper function