Refactor and add Darwin support