Rewrote to make it more compact.