Solution for bonus problem