added detailed comments explaining the code