Re-written. The old solution was verbose and incomplete