Tried to simplify and unify
Tried to further simplify and added some comments