Working solution to the bonus problem