Refactor code, improve comments