Improve comments and generalize the code