add comments and edge cases
sol of q097