Solution for problems 5 & 6