added comments and improved readabitly by indenting