cleanup code and add comments