add comments and refractor code