Code refactor and improve comments