Add comments
Add comments. Move some logic into a dedicated function for readability