SPARK-1170. Merged commits and fixed bugs in both the original commits