Added more comments and renamed function