Same features as previous commit with minor bug fixes.