Add comments and refactor code