Cleaned up implementation a bit.