To be updated with changes from the answer below.