Simplified code by using a list comprehension

Added more documentation.