Incorporating some bug fixes from KAGGLE